Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
How can I put individual "p" elements into the "container" div using js function?

I tried to put "p" elements() into the "div"(container) elements but failed -->

let t = [
    "First line!",
    "Second line!",
    "Third line!",
    "Fourth line!"
];

let T = t.length;

function load(){
    for(let i = 0; i < T; i++){
        let contElem = document.createElement("div");
        contElem.setAttribute("class", "container");
        contElem.setAttribute("onload", "load02()");
        document.querySelector("#main").appendChild(contElem);
    }
}

function load02(){
    for(let i = 0; i < T; i++){
        let myElem = document.createElement("p");
        myElem.innerHTML = t[i];
        document.querySelector(".container").appendChild(myElem);
    }
}

but it did not work! Help!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You're attribute onload will never fire ... Since the DOM has already "loaded" at this point .. You just need to call the function .. And call load2() at the end of function 1

let t = [
    "First line!",
    "Second line!",
    "Third line!",
    "Fourth line!"
];

let T = t.length;
load();  // RUN YOUR FUNCTION
function load(){
    for(let i = 0; i < T; i++){
        let contElem = document.createElement("div");
        contElem.setAttribute("class", "container");
        //contElem.setAttribute("onload", "load2()"); // REMOVE THIS
        document.querySelector("#main").appendChild(contElem);
    }
    load2();  //ADD THIS
}

function load2(){
    for(let i = 0; i < T; i++){
        let myElem = document.createElement("p");
        myElem.innerHTML = t[i];
        document.querySelector(".container").appendChild(myElem);
    }
}
.container{
  border:1px solid #FF0000;
  height:50px;
  width:50px;
}
<div id="main">

</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

let t = [
  "First line!",
  "Second line!",
  "Third line!",
  "Fourth line!"
];

let T = t.length;

function load() {
  for (let i = 0; i < T; i++) {
    let contElem = document.createElement("div"), myElem = document.createElement("p");
    contElem.setAttribute("class", "container");
    myElem.innerHTML = t[i];
    contElem.appendChild(myElem);
    document.querySelector("#main").appendChild(contElem);
  }
}

load();
<div id="main"></div>

Does that work? Or were you wanting to put all four p elements in each containing div element?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda